package com.yu.subject.application.controller;

import com.yu.subject.application.entity.SubjectLabelDTO;
import com.yu.subject.common.entity.Result;
import com.yu.subject.common.exception.CodeAndMessage;
import com.yu.subject.common.exception.JavaSmException;
import com.yu.subject.common.util.BeanCopyUtil;
import com.yu.subject.domain.entity.SubjectLabelBO;
import com.yu.subject.domain.service.SubjectLabelDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 路亚
 * @version 0.1
 * @className SubjectLabelController
 * @description
 * @date 2024/12/23 22:14
 * @since jdk11
 */
@RestController
@RequestMapping("/subject/label")
@Slf4j
@RequiredArgsConstructor
public class SubjectLabelController {

    private final SubjectLabelDomainService subjectLabelDomainService;

    /**
     * 新增标签
     */
    @PostMapping("/add")
    public Result add(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if (log.isInfoEnabled()){
            log.info("新增题目标签，参数：{}", subjectLabelDTO);
        }
        if (StringUtils.isEmpty(subjectLabelDTO.getLabelName())){
            throw new JavaSmException(CodeAndMessage.LABEL_NAME_NOT_NULL);
        }
        SubjectLabelBO subjectLabelBO = new SubjectLabelBO();
        BeanUtils.copyProperties(subjectLabelDTO, subjectLabelBO);
        subjectLabelDomainService.add(subjectLabelBO);
        return Result.ok(true);
    }

    /**
     * 更新标签
     */
    @PostMapping("/update")
    public Result update(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if (log.isInfoEnabled()){
            log.info("更新题目标签，参数：{}", subjectLabelDTO);
        }
        if (subjectLabelDTO.getId() == null){
            throw new JavaSmException(CodeAndMessage.LABEL_ID_NOT_NULL);
        }
        SubjectLabelBO subjectLabelBO = new SubjectLabelBO();
        BeanUtils.copyProperties(subjectLabelDTO, subjectLabelBO);
        subjectLabelDomainService.updateById(subjectLabelBO);
        return Result.ok(true);
    }

    /**
     * 删除标签
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if (log.isInfoEnabled()){
            log.info("删除题目标签，参数：{}", subjectLabelDTO);
        }
        if (subjectLabelDTO.getId() == null){
            throw new JavaSmException(CodeAndMessage.LABEL_ID_NOT_NULL);
        }
        subjectLabelDomainService.deleteById(subjectLabelDTO.getId());
        return Result.ok(true);
    }

    /**
     * 根据分类查询标签
     */
    @GetMapping("/queryLabelByCategoryId")
    public Result queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if (log.isInfoEnabled()){
            log.info("根据分类查询标签，参数：{}", subjectLabelDTO.getCategoryId());
        }
        if (subjectLabelDTO.getCategoryId() == null){
            throw new JavaSmException(CodeAndMessage.CATEGORY_ID_NOT_NULL);
        }
        List<SubjectLabelBO> labelBOList = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelDTO.getCategoryId());
        List<SubjectLabelDTO> result = BeanCopyUtil.copyListProperties(labelBOList, SubjectLabelDTO.class);
        return Result.ok(result);
    }
}
